import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class JedisSet {

    private static void test1(Jedis jedis) {
        System.out.println("-------------------------");
        System.out.println("sadd 和 smembers");
        jedis.flushAll();

        jedis.sadd("key1", "m1", "m2", "m3");
        Set<String> set = jedis.smembers("key1");

        System.out.println(set);
    }

    private static void test2(Jedis jedis) {
        System.out.println("-------------------------");
        System.out.println("sismember");
        jedis.flushAll();

        jedis.sadd("key1", "m1", "m2", "m3");
        boolean result1 = jedis.sismember("key1", "m2");
        boolean result2 = jedis.sismember("key1", "m100");

        System.out.println("result1: " + result1);
        System.out.println("result2: " + result2);
    }

    private static void test3(Jedis jedis) {
        System.out.println("-------------------------");
        System.out.println("scard");
        jedis.flushAll();

        jedis.sadd("key1", "m1", "m1", "m3");
        long result = jedis.scard("key1");

        System.out.println("result: " + result);
    }

    private static void test4(Jedis jedis) {
        System.out.println("-------------------------");
        System.out.println("spop");
        jedis.flushAll();

        jedis.sadd("key1", "m1", "m1", "m3");
        String result = jedis.spop("key1");

        System.out.println("result: " + result);
    }

    private static void test5(Jedis jedis) {
        System.out.println("-------------------------");
        System.out.println("sinter");
        jedis.flushAll();

        jedis.sadd("key1", "m1", "m2", "m3");
        jedis.sadd("key2", "m2", "m3", "m4");
        Set<String> set = jedis.sinter("key1", "key2");

        System.out.println(set);
    }

    private static void test6(Jedis jedis) {
        System.out.println("-------------------------");
        System.out.println("sinter");
        jedis.flushAll();

        jedis.sadd("key1", "m1", "m2", "m3");
        jedis.sadd("key2", "m2", "m3", "m4");
        long len = jedis.sinterstore("keyStore", "key1", "key2");

        System.out.println("len: " + len);
        System.out.println("keyStore: " + jedis.smembers("keyStore"));
    }


    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis = jedisPool.getResource()) {
            test1(jedis);
            test2(jedis);
            test3(jedis);
            test4(jedis);
            test5(jedis);
            test6(jedis);
        }
    }

}
